package com.j180.controller.stoGoods;

import com.alibaba.druid.support.json.JSONUtils;
import com.j180.bean.PageBean;
import com.j180.bean.ResultBean;
import com.j180.bean.StoGoods;
import com.j180.service.stoGoods.IStoGoodsService;
import com.j180.util.UploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/stoGoods")
public class StoGoodsController {

    @Autowired
    private IStoGoodsService stoGoodsService;

    // map  curPage当前页数  pageSize每页显示条数 id门店id
    @GetMapping("/showAllStoGoods/{condition}")
    public ResultBean<PageBean<StoGoods>> getAllStoGoods(@PathVariable("condition") String condition) {

        Map<String,Object> map = (Map<String, Object>) JSONUtils.parse(condition);
        ResultBean<PageBean<StoGoods>> resultBean = new ResultBean<>();
        PageBean<StoGoods> pageBean = stoGoodsService.getAllStoGoods(map);
        if (pageBean != null) {
            resultBean.setCode(1);
            resultBean.setT(pageBean);
            resultBean.setMsg("ok");
        } else {
            resultBean.setCode(0);
            resultBean.setT(null);
            resultBean.setMsg("no");
        }
        return resultBean;
    }


    // 访问路径：http://localhost:8081/pet/stoGoods/showStoGoods?storeId=1&stoGoodsId=1
    // 使用map接收参数 必须设置@RequestParam
    // Map不能用来接受复合参数，也就是 接收简单的键值对
    // 这里接收的是 "storeId":1,"stoGoodsId":1    这里可以直接使用 @GetMapping("/showStoGoods")
    @RequestMapping("/showStoGoods")
    @ResponseBody
    public StoGoods getStoGoodsByStoreIdAndStoGoodsId(@RequestParam Map<String,Object> map){
        StoGoods stoGoods = stoGoodsService.getStoGoodsByStoreIdAndStoGoodsId(map);
        System.out.println(map);
        return stoGoods;
    }

    @PostMapping("/addStoGoods")
    public ResultBean<StoGoods> addStoGoods(@RequestBody(required = false) StoGoods stoGoods,
                                            @RequestParam(value = "photo",required = false) CommonsMultipartFile file) {
        ResultBean<StoGoods> resultBean = new ResultBean<>();
        String newFileName = UploadFileUtil.newFileName(file);

        // if (!"".equals(newFileName) && newFileName != null) {
        //     stoGoods.setPic(newFileName);
        if (!"".equals(newFileName)) {
            int count = stoGoodsService.addStoGoods(stoGoods);
            if (count > 0) {
                //上传文件
                // UploadFileUtil.uploadFile(file, "images\\stoGoods");
                resultBean.setCode(1);
                resultBean.setT(stoGoods);
                resultBean.setMsg("ok");
            }
        } else {
            resultBean.setCode(0);
            resultBean.setT(null);
            resultBean.setMsg("no");
        }
        return resultBean;
    }

    @DeleteMapping("deleteStoGoods/{id}")
    public ResultBean<StoGoods> deleteStoGoods(@PathVariable("id") int id){
        ResultBean<StoGoods> resultBean = new ResultBean<>();
        int count = stoGoodsService.deleteStoGoodsByStoGoodsId(id);
        if (count != 0){
            resultBean.setCode(1);
            resultBean.setMsg("ok");
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("no");
        }
        return resultBean;
    }

    @PutMapping("/updateStoGoods")
    public ResultBean<StoGoods> updateStoGoods(@RequestBody(required = false) StoGoods stoGoods,
                                               @RequestParam(value = "photo",required = false) CommonsMultipartFile file){
        ResultBean<StoGoods> resultBean = new ResultBean<>();

        String newFileName = UploadFileUtil.newFileName(file);
        // if (!"".equals(newFileName) && newFileName != null){
        //     stoGoods.setPic(newFileName);
        if (!"".equals(newFileName)){
            int count = stoGoodsService.updateStoGoods(stoGoods);
            if(count>0){
                //上传文件
                // UploadFileUtil.uploadFile(file,"images\\stoGoods");
                resultBean.setCode(1);
                resultBean.setT(stoGoods);
                resultBean.setMsg("ok");
            }
        } else {
            resultBean.setCode(0);
            resultBean.setT(null);
            resultBean.setMsg("no");
        }
        return resultBean;
    }

    @GetMapping("/getStoGoodsByStoGoodsName/{stoGoodsName}")
    public ResultBean<List<StoGoods>> getStoGoodsByStoGoodsName(@PathVariable("stoGoodsName") String name) {
        ResultBean<List<StoGoods>> resultBean = new ResultBean<>();
        List<StoGoods> stoGoodsList = stoGoodsService.getStoGoodsByStoGoodsName(name);
        if (stoGoodsList != null) {
            resultBean.setCode(1);
            resultBean.setT(stoGoodsList);
            resultBean.setMsg("ok");
        } else {
            resultBean.setCode(0);
            resultBean.setT(null);
            resultBean.setMsg("no");
        }
        return resultBean;
    }
}